//
//  YWSingleSelectEquSystemTypeTableViewController.m
//  maintain
//
//  Created by 烟雨云渊 on 2017/5/11.
//  Copyright © 2017年 ewin. All rights reserved.
//

#import "YWSingleSelectEquSystemTypeTableViewController.h"
#import "UIColor+CustomColors.h"
#import "YWMacro.h"

#import "YWEquipmentType.h"
#import "YWEquipmentManager.h"
@interface YWSingleSelectEquSystemTypeTableViewController ()
@property (nonatomic, strong) NSMutableArray *allEquipmentSystemType;
@end

@implementation YWSingleSelectEquSystemTypeTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"报障类别";
    self.tableView.backgroundColor = [UIColor customBackgroudColor];
    self.tableView.tableFooterView = [[UIView alloc]init];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(sureButtonEvent)];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSMutableArray *)allEquipmentSystemType{
    if (!_allEquipmentSystemType) {
        YWEquipmentSystemType * equipmentSystemType = [[YWEquipmentSystemType alloc] init];
        equipmentSystemType.systemTypeName = @"无";
        _allEquipmentSystemType = [NSMutableArray array];
        [_allEquipmentSystemType addObject:equipmentSystemType];
        [_allEquipmentSystemType addObjectsFromArray:[[YWEquipmentManager shareManager] allEquipmentSystemType]];
    }
    return _allEquipmentSystemType;
}
#pragma mark - Table view data source


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return [self.allEquipmentSystemType count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellId  = @"equipmentSystemTypeReuseIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.textLabel.font = kContentFont;
    }
    YWEquipmentSystemType *sysTemType = self.allEquipmentSystemType[indexPath.row];
    cell.textLabel.text = sysTemType.systemTypeName?:@"";
    if (sysTemType.systemTypeId == _currentEquipmentSystemType.systemTypeId) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }else{
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    return cell;
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return @"请选择报障类别";
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    YWEquipmentSystemType *sysTemType = self.allEquipmentSystemType[indexPath.row];
    _currentEquipmentSystemType = sysTemType;
    [self.tableView reloadData];
}

- (void)sureButtonEvent{
    if ([self.delegate respondsToSelector:@selector(popSelectedEquipmentSystemType:)]) {
        [self.delegate popSelectedEquipmentSystemType:_currentEquipmentSystemType];
        [self.navigationController popViewControllerAnimated:YES];
    }
}

@end
